package Test04;

public class SequenceQueue<T> {
    final  int MAX_CAPACITY=10;
    private  T queueArray[];
    private  int front,rear;

    public SequenceQueue() {
        this.front = this.rear = 0;
        queueArray=(T[]) new  Object[this.MAX_CAPACITY];
    }
    public void  EnQueue(T obj){
            if((rear+1)%queueArray.length == front){
                T[] p = (T[])new  Object[queueArray.length*2];
                if(rear ==((T[])queueArray).length-1){
                    for (int i = 1; i <= rear; i++) {
                        p[i] = queueArray[i];
                    }
                }else {
                    int i,j=1;
                    for (i = front+1; i < queueArray.length; i++,j++) {
                        p[j] = queueArray[i];
                    }
                    for (i=0; i <= rear;i++,j++) {
                        p[j] = queueArray[i];
                    }
                    front = 0;
                    rear=queueArray.length-1;
                }
                queueArray = p;
            }
            rear=(rear+1)%queueArray.length;
            queueArray[rear]=obj;
    }
    public T  DeQueue(){
        if(isEmpty()){
            System.out.println("队列已空,无法出队!");
            return null;
        }
        front = (front+1)%queueArray.length;
        return  queueArray[front];
    }
    public  T getHead(){
        if(isEmpty()){
            System.out.println("队列已空,无法读取元素!");
            return null;
        }
        return queueArray[(front+1)%queueArray.length];
    }
    public int size(){
        return  (rear-front+queueArray.length) % queueArray.length;
    }
    public boolean isEmpty(){
        return front==rear;
    }
    public  void  nextOrder(){
        int i,j=front;
        for (i = 0; i <= size(); i++) {
            j=(j+1)%queueArray.length;
            System.out.println(queueArray[j]);
        }
    }
    public  void  clear(){
        front=rear=0;
    }
}

